package com.yikai.p3.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yikai.p3.entity.ResultDTO;
import com.yikai.p3.entity.User;
import com.yikai.p3.entity.UserLoginDTO;
import com.yikai.p3.service.UserService;
import com.yikai.p3.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/user", produces = "application/json;charset=utf-8")
public class UserController {

    @Resource
    private RedisTemplate<String, User> redisTemplate;


    @Autowired
    private UserService userService;

    @Resource
    private JwtUtil jwtUtil;

//    @PostMapping("/register")
//    public ResultDTO register(@RequestBody UserRegister userRegister){
//
//    }

    @GetMapping("/userInfo")
    public ResultDTO userInfo(@RequestHeader(JwtUtil.HEADER) String token){
        User user = redisTemplate.opsForValue().get(token);
        return ResultDTO.success(user);
    }


    @PostMapping ("/login")
    public ResultDTO login(@RequestBody @Validated UserLoginDTO userLoginDTO, HttpServletResponse response){
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));

        if (user == null){
            return ResultDTO.error("用户信息不存在");
        }

        if(!user.getPassword().equals(password)){
            return ResultDTO.error("用户名或密码错误");
        }

        String token = jwtUtil.generateToken(username);

        // 把用户信息存到redis里
        redisTemplate.opsForValue().set(token,user);


        response.setHeader(JwtUtil.HEADER, token);
        response.setHeader("Access-control-Expost-Headers", JwtUtil.HEADER);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        return ResultDTO.success(map);
    }

    @GetMapping("/logout")
    public ResultDTO logout(HttpServletRequest request, HttpServletResponse response) {
        // 退出登录
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            //清除认证
            new SecurityContextLogoutHandler().logout(request, response, auth);
            // 从redis里删掉
            redisTemplate.delete(request.getHeader(JwtUtil.HEADER));
        }
        return ResultDTO.success();
    }
}
